<template>
  <div>
    <!-- 面包屑区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单商品</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索区域 -->
      <el-input placeholder="请输入内容" v-model="ordersinfo.query" clearable class="input-with-select" @clear="getOrdersList">
        <el-button slot="append" icon="el-icon-search" @click="getOrdersList"></el-button>
      </el-input>
      <el-table border stripe :data="ordersList" style="width: 100%" max-height="465">
        <el-table-column type="index" label="#"> </el-table-column>
        <el-table-column prop="order_number" label="订单编号" width="350"> </el-table-column>
        <el-table-column prop="order_price" label="订单价格(元)"> </el-table-column>
        <el-table-column label="是否付款">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.order_pay - 0">已付款</el-tag>
            <el-tag type="danger" v-else>未付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货"> </el-table-column>
        <el-table-column label="下单时间" width="200">
          <template slot-scope="scope">
            {{ scope.row.create_time | formatTimestamp }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150">
          <el-button type="primary" icon="el-icon-edit" size="mini" @click="openAddrDialog"></el-button>
          <el-button type="success" icon="el-icon-location-outline" size="mini" @click="openLogisticsDialog"></el-button>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="ordersinfo.pagenum" :page-sizes="[6, 10, 15, 27]" :page-size="ordersinfo.pagesize" layout="total, sizes, prev, pager, next, jumper" :total="total"> </el-pagination>
    </el-card>
    <!-- 修改地址对话框 -->
    <el-dialog title="修改地址" :visible.sync="addrDialogVisible" width="50%" @close="clearDialog">
      <el-form :model="addrRuleForm" :rules="addrRuleFormRules" ref="addrRuleFormRef" label-width="100px">
        <el-form-item label="省市区/县" prop="address">
          <el-cascader v-model="addrRuleForm.addderss" :props="cascaderProps" :options="citydata" @change="handleChange"></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="fullAddress">
          <el-input v-model="addrRuleForm.fullAddress"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="addrDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addrDialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 物流查询对话框 -->
    <el-dialog title="物流查询" :visible.sync="logisticsDialogVisible" width="50%">
      <!-- 时间线组件 -->
      <el-timeline :reverse="reverse">
        <!-- timestamp: 时间 -->
        <el-timeline-item v-for="(activity, index) in progressList" :key="index" :timestamp="activity.time">
          {{ activity.context }}
        </el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
import commonAPI from '@/api/commonAPI/commonAPI.js'
// 导入中国省市县数据
import citydata from '@/views/Orders/citydata.js'
// 导入物流假数据
import progress from '@/views/Orders/progress.js'
export default {
  name: 'orders',
  data() {
    return {
      ordersinfo: {
        query: '', // 查询的参数
        pagenum: 1, // 当前页
        pagesize: 3 // 每页显示的数据条数
      },
      // 订单数据
      ordersList: [],
      // 总条数
      total: 0,
      // 是否显示修改地址对话框
      addrDialogVisible: false,
      // 中国各省市县的地址
      citydata,
      // 表单数据对象
      addrRuleForm: {
        address: [],
        fullAddress: ''
      },
      // 表单数据的验证规则
      addrRuleFormRules: {
        address: [{ required: true, message: '请填入省市区/县', trigger: 'blur' }],
        fullAddress: [{ required: true, message: '请填入详细地址', trigger: 'blur' }]
      },
      // 级联选择器的配置对象(不写默认显示数据是 数据中的 value, label, children属性)
      cascaderProps: {
        expandTrigger: 'hover',
        value: 'value', // 选中的值
        label: 'label', // 展示的值
        childrren: 'childrren' // 指定父子级的嵌套属性
      },
      // 是否显示物流查询对话框
      logisticsDialogVisible: false,
      // 物流信息
      progressList: progress,
      // 时间线组件使用倒序
      reverse: true
    }
  },
  created() {
    this.getOrdersList()
  },
  methods: {
    // 点击 由clearable属性导致的输入框清空时触发 // 点击搜索按钮时触发 // vue的created钩子阶段触发触发
    async getOrdersList() {
      const { data: res } = await commonAPI({ method: 'get', url: 'orders', params: this.ordersinfo })
      if (res.meta.status !== 200) return this.$message.error('请求订单数据失败!')
      this.$message.success('请求订单数据成功!')
      this.ordersList = res.data.goods
      this.total = res.data.total
    },
    // 当改变每页显示多少条数据时触发
    handleSizeChange(size) {
      this.ordersinfo.pagesize = size
      this.getOrdersList()
    },
    // 当页码值时触发
    handleCurrentChange(num) {
      this.ordersinfo.pagenum = num
      this.getOrdersList()
    },
    // 点击修改地址对话框时显示
    openAddrDialog() {
      this.addrDialogVisible = true
    },
    // 当级联选择器的选中项发生改变时触发
    handleChange() {},
    // 当点击打开物流查询按钮时触发
    async openLogisticsDialog() {
      this.logisticsDialogVisible = true
      // 物流接口损坏(故不再请求)，自己写的假数据
    },
    // 关闭物流对话框时触发
    clearDialog() {
      this.$refs.addrRuleFormRef.resetFields()
      this.addrRuleForm = { address: [], fullAddress: '' }
    }
  }
}
</script>

<style lang="less" scoped>
.input-with-select {
  margin-bottom: 20px;
  width: 35%;
}
.el-cascader {
  width: 100%;
}
</style>
